package com.it.gw.modules.station.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.StringUtils
import com.it.data.network.rsp.WtGQVo
import com.it.gw.R
import com.it.gw.databinding.ItemOprationRecordBinding


class GQStationRecordAdapter(
    var context: Context,
    var listener: (WtGQVo) -> Unit,
    var datas: List<WtGQVo>
) : RecyclerView.Adapter<GQStationRecordAdapter.ViewHolder>() {
    companion object {
        const val TAG = "GQStationListAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemOprationRecordBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.username.text = StringUtils.getNoNullData(data.queUsername)
        holder.binding.createtime.text = StringUtils.getNoNullData(data.createtime)
        holder.binding.stnm.text = StringUtils.getNoNullData(data.stnm)

        holder.binding.dataName.text = StringUtils.getNoNullData(data.dataName)
        holder.binding.trnm.text = StringUtils.getNoNullData(data.trnm)


        if (data.queDeal.isNotEmpty() && data.queDeal?.length == 3) {
            holder.binding.QUEDEAL.text = "${data.queDeal?.substring(0, 1)}\n${
                data.queDeal?.substring(
                    1,
                    2
                )
            }\n${data.queDeal?.substring(2, 3)}"
        } else {
            holder.binding.QUEDEAL.text = StringUtils.getNoNullData(data.queDeal)
        }
        if (data.queDeal == "已解决") {
            holder.binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_green)
        } else {
            holder.binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_yellow)
        }
        holder.binding.root.setOnClickListener {
            listener.invoke(data)
        }
    }


    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemOprationRecordBinding) : RecyclerView.ViewHolder(binding.root)
}